package lt.ntec.danblog.server.core.validator.rules;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import lt.ntec.danblog.server.core.validator.Rule;

public abstract class PatternRule implements Rule {

    @Override
    public boolean isValid(Object input) {
        if (!(input instanceof String)) {
            return false;
        }
        boolean isValid = false;
        String expression = getPattern();
        CharSequence inputStr = (String) input;
        Pattern pattern = Pattern.compile(expression);
        Matcher matcher = pattern.matcher(inputStr);
        if (matcher.matches()) {
            isValid = true;
        }
        return isValid;
    }

    protected abstract String getPattern();
}
